#!/usr/bin/python

"""Project Euler Solution 028

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile

def get_answer():
    """Question:
    Starting with the number 1 and moving to the right in a clockwise direction
    a 5 by 5 spiral is formed as follows:

    21 22 23 24 25
    20  7  8  9 10
    19  6  1  2 11
    18  5  4  3 12
    17 16 15 14 13
    
    It can be verified that the sum of the numbers on the diagonals is 101.
    
    What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral 
    formed in the same way?

    
    """
    
    #The length of the side of the spiral.
    spiral_length_of_side = 1001
        
    def sum_layer(layer_number):
        """Returns the total for the layer given by [layer_number]. The layer
        refers to the set of numbers which form a square surrounding the kernel
        of the spiral. In the example the below, the cells marked with an
        x form the second layer (layer_number = 2), the cells marked with a y 
        form the third layer (layer_number = 3), while the cells marked with
        a z form the fourth layer (layer_number = 4).
        
        zzzzzzz
        zyyyyyz
        zyxxxyz
        zyx0xyz
        zyxxxyz
        zyyyyyz
        zzzzzzz
        """
        
        if layer_number == 1:
            return 1
        
        #The length of the side for the current layer.
        length_of_side = (layer_number * 2 - 1);
        
        #The number at the top right corner of the layer.
        max_number = length_of_side ** 2
        
        #The difference between a corner in this layer, and the corner
        #following it in the spiral.
        difference = length_of_side - 1
        
        #The number at the bottom right corner of the layer. 
        min_number = max_number - difference * 4
        
        #Return the total of all four numbers in the corners of the layer.
        return sum(xrange(max_number, min_number, -difference))
    
    #Return result.
    return sum(
              sum_layer(layer_number) 
                for layer_number in xrange(1, (spiral_length_of_side + 3) / 2)
          )
    
if __name__ == "__main__":
    cProfile.run("print(get_answer())")
